package get

import (
	"database/sql"
	"fmt"

	"github.com/gin-gonic/gin"
	_ "github.com/lib/pq"
)

const (
	host     = "localhost"
	port     = 5432
	user     = "postgres"
	password = "postgres"
	dbname   = "MBTI"
)

func Getaccount(r *gin.Engine) {
	var err error
	var db *sql.DB
	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
		"password=%s dbname=%s sslmode=disable",
		host, port, user, password, dbname)
	db, err = sql.Open("postgres", psqlInfo)
	var wusername, wuserpassword string
	r.GET("/login", func(c *gin.Context) {
		// 获取参数
		wusername = c.Query("username")
		wuserpassword = c.Query(("userpassword"))
		c.JSON(200, gin.H{
			"username":     wusername,
			"userpassword": wuserpassword,
		})

	})

	fmt.Printf("%s%s", wusername, wuserpassword)

	r.GET("/loginsuccess", func(c *gin.Context) {
		var code, str, username, userpassword string
		code = "0"

		if err != nil {
			panic(err)
		}

		str = "SELECT userpassword FROM public.web where username='" + wusername + "'"
		rows, err := db.Query(str)
		if err != nil {
			panic(err)
		}
		for rows.Next() {
			err = rows.Scan(&userpassword)
			if err != nil {
				panic(err)
			}
		}
		if wuserpassword == userpassword {
			code = "10"
		}
		if wusername == "admin" && wuserpassword == "123456" {
			code = "20"
		}
		fmt.Printf("user:%s%s", username, userpassword)
		// 获取参数
		c.JSON(200, gin.H{
			"code":        code,
			"str":         str,
			"webpassword": wuserpassword,
			"password":    userpassword,
		})
	})
}
